package com.ruoyi.common.utils;


import com.alibaba.fastjson2.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
public class AliyunSmsUtil {
    public  boolean sendSms(String phoneNumber, String templateCode, HashMap<String, Object> templateParams) {
        try {
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5t7YDCjU8QN66rjKwTxw", "4THrG3MOAd7vgLjKlIyK4Pf4dZuIKD");
            IAcsClient client = new DefaultAcsClient(profile);

            SendSmsRequest request = new SendSmsRequest();
            request.setPhoneNumbers(phoneNumber);
            request.setSignName("檬橙校园");
            request.setTemplateCode(templateCode);
            // 将HashMap转化为JSON字符串
            String templateParam = JSON.toJSONString(templateParams);
            request.setTemplateParam(templateParam);
            SendSmsResponse response = client.getAcsResponse(request);
            return "OK".equals(response.getCode());
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }
    }
    public  boolean noticeSms(String phone, String name, String time) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("name", name);
        hashMap.put("time", time);
        // 4. 调用阿里云接口发送短信，将验证码发送给指定的手机号码
        return sendSms(phone, "SMS_465730667", hashMap);
    }
    @Test
    public void testSms(){
        System.out.println(remindSms("17677203072", "一个普普通通的校园活动"));
    }

    public  boolean  remindSms(String phone, String name){
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("name", name);
        return sendSms(phone, "SMS_465735669", hashMap);
    }
}

